<template>
    <view class="container">
        <view class="loading" v-if="loading">登录中...</view>
        <view class="error" v-if="error">{{ errorMsg }}</view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            loading: true,
            error: false,
            errorMsg: '',
			referrer_id: 0,
        }
    },
    onLoad(options) {
		this.referrer_id = options.referrer_id;
        console.log(options)
        // return
        const code = options.code;
        const state = options.state;
        
        if (!code || state !== 'STATE') {
            this.error = true;
            this.errorMsg = '授权失败，请重试';
            this.loading = false;
            return;
        }
        
        // 调用后端接口完成登录
        this.$myRuquest({
            url: '/index/auth/callback',
            method: 'GET',
            data: {
                code: code,
                state: state,
                referrer_id: this.referrer_id,
            }
        }).then(res => {
            if (res.data.code === 200) {
                // 存储用户信息到缓存
                uni.setStorageSync('my_info', res.data.data);
                
                // 登录成功，跳转到首页
                setTimeout(() => {
                    uni.switchTab({
                        url: '/pages/index/index'
                    });
                }, 400);
            } else {
                this.error = true;
                this.errorMsg = res.data.info || '登录失败';
                this.loading = false;
            }
        }).catch(err => {
            this.error = true;
            this.errorMsg = '网络错误，请重试';
            this.loading = false;
            console.error('登录错误:', err);
        });
    }
}
</script>

<style>
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
}

.loading {
    font-size: 16px;
    color: #333;
}

.error {
    font-size: 16px;
    color: #f44336;
}
</style>